<template>
	<view class="select-page">
		<view class="item" v-for="(item, index) in teamLists" :key="item.teamId">
			<image :src="teamUrl" mode="aspectFit" class="item-team-img"></image>
			<text>{{item.teamName}} （{{item.count}}）</text>
			<view class="btn" @click="onSelect(item)">选择</view>
		</view>
	</view>
</template>

<script>
	import {
	    mapState,
	    mapMutations,
	    mapActions
	} from 'vuex';
	import hwOperatorLocationApi from '@/api/hwOperator/hwOperatorLocationApi'

	export default{

		data() {
			return {
				teamUrl: this.$appImageUrl+"/xcx/ic_team_pic.png",
				teamLists: [],
				noSelect: true
			}
		},
		onLoad() {
			this.getTeam()
		},
		methods: {
			...mapMutations(['SET_OPERATOR_TEAM_INFO']),
			onSelect(item) {
				if (this.noSelect){
					this.noSelect = false
					this.SET_OPERATOR_TEAM_INFO(item)
					uni.navigateBack({			
					})
				}
				
			},
			getTeam() {
				let that = this
				hwOperatorLocationApi.hwOperatorRealTime({
					
				}).then((res) => {
					if (res.code == 200) {
						var list = res.data.operatorList
						this.teamLists.splice(0, this.teamLists.length)
						for (var i in list){
							var item = list[i]
							if (!(item.teamId&&item.teamId.length>0)){
								item.teamId = "other"
								item.teamName = "其他"
							}
							var index = this.teamLists.findIndex(t=>{
								if (item.teamId == t.teamId){
									t.count++
									return true
								}else{
									return false
								}
							})
							if (index<0){
								var obj = {
									teamId: item.teamId,
									teamName: item.teamName,
									count: 1,
								}
								this.teamLists.push(obj)
							}		
						}
						this.teamLists.sort((item) => {
							return item.teamId == 'other' ? 1 : -1
						})
					}	
				})	
			}
		}
	}
</script>

<style lang="scss" scoped>
	.select-page{
		padding: 0 24rpx;
	}
	.tree-tit{
		width: 702rpx;
		height: 98rpx;
		padding: 0 24rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		color: #333333;
		background: #F9F9FA;
		font-size: 28rpx;
		position: fixed;
		top: 0;
		left: 0;
	}
	.item{
		height: 108rpx;
		display: flex;
		align-items: center;
		padding: 0 36rpx;
		background: #FFFFFF;
		border: 1px solid #E5E5E6;
		border-radius: 8rpx;
		font-size: 28rpx;
		margin-top: 12rpx;
	}
	.item-team-img{
		width: 42rpx;
		height: 34rpx;
		margin-right: 36rpx;
	}
	.item text{
		flex: 1;
	}
	.btn{
		width: 120rpx;
		height: 58rpx;
		text-align: center;
		line-height: 58rpx;
		background: #FFFFFF;
		color: #3791E9;
		border: 1px solid #3791E9;
		border-radius: 8rpx;
	}
</style>
